﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_user_ViewUser : System.Web.UI.Page
{
    User currenuser = UserManagement.GetCurrentUser();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ltrRole.Text = "hide";
            ltrtextSearch.Text = "hide";
            ltrProjecID.Text = "hide";
            ltrBugID.Text = "hide";
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string text = txtSearch.Text.Trim();
        var user = from item in ApplicationGlobalVariables.KillDaBugsDatabase.Users
                   where item.Username.Contains(text) || item.Fullname.Contains(text) || item.Email.Contains(text) || item.Organization.Contains(text)
                   select new
                  {
                      item.Username,
                      item.Fullname,
                      item.IsActive,
                      //  item.User.RecentLoginDate,
                      item.AvatarImage,
                      item.Email,
                      item.Organization,
                      //  item.User.EnableEmailNotification,
                      //   item.BugsPerPage,
                      item.Role
                  };
        LVDisplay.DataSource = user;
        LVDisplay.DataBind();
    }

    protected void ddlProjectID_SelectedIndexChanged(object sender, EventArgs e)
    {
        int idproject = Convert.ToInt32(ddlProjectID.SelectedItem.Value);
        var user = from item in ApplicationGlobalVariables.KillDaBugsDatabase.User_Projects
                   where item.ProjectId == idproject
                   select new
                   {
                       item.User.Username,
                       item.User.Fullname,
                       item.User.IsActive,
                       //  item.User.RecentLoginDate,
                       item.User.AvatarImage,
                       item.User.Email,
                       item.User.Organization,
                       //  item.User.EnableEmailNotification,
                       //   item.User.BugsPerPage,
                       item.User.Role
                   };
        LVDisplay.DataSource = user;
        LVDisplay.DataBind();
    }

    protected void ddlBUgID_SelectedIndexChanged(object sender, EventArgs e)
    {
        int idbug = Convert.ToInt32(ddlBugID.SelectedItem.Value);
        var user = from item in ApplicationGlobalVariables.KillDaBugsDatabase.User_Bugs
                   where item.BugId == idbug
                   select new
                   {
                       item.User.Username,
                       item.User.Fullname,
                       item.User.IsActive,
                       //  item.User.RecentLoginDate,
                       item.User.AvatarImage,
                       item.User.Email,
                       item.User.Organization,
                       //  item.User.EnableEmailNotification,
                       //   item.User.BugsPerPage,
                       item.User.Role
                   };
        LVDisplay.DataSource = user;
        LVDisplay.DataBind();
    }

    protected void ddlSearchFor_SelectedIndexChanged(object sender, EventArgs e)
    {
        switch (ddlSearchFor.SelectedItem.Value)
        {
            case "UserName": { SearchAnother(); break; }
            case "Project Name": SearchProjectID(); { break; }
            case "BugID": SearchBugID(); { break; }
            case "FullName": { SearchAnother(); break; }
            case "Role": { SearchRole(); break; }
            case "Organization": { SearchAnother(); break; }
            case "Email": { SearchAnother(); break; }
        };
    }

    private void SearchProjectID()
    {
        LVDisplay.DataSource = null;
        LVDisplay.DataBind();

        ltrtextSearch.Text = "hide";
        ltrProjecID.Text = "";
        ltrBugID.Text = "hide";
        ltrRole.Text = "hide";

        var ProjectID = from item in ApplicationGlobalVariables.KillDaBugsDatabase.User_Projects.Where(i => i.Username == currenuser.Username)
                        select item.Project;
        ddlProjectID.Items.Clear();
        foreach (var item in ProjectID)
        {
            //  ddlProjectID.Items.Add(item.ToString());
            ListItem lt = new ListItem(item.Name, item.ProjectId.ToString());
            ddlProjectID.Items.Add(lt);
        }
    }

    private void SearchBugID()
    {
        LVDisplay.DataSource = null;
        LVDisplay.DataBind();
        ltrtextSearch.Text = "hide";
        ltrProjecID.Text = "hide";
        ltrBugID.Text = "";
        ltrRole.Text = "hide";
        var bugid = from item in ApplicationGlobalVariables.KillDaBugsDatabase.User_Bugs.Where(i => i.Username == currenuser.Username)
                    select item.Bug.BugId;
        ddlBugID.Items.Clear();
        foreach (var item in bugid)
        {
            ddlBugID.Items.Add(item.ToString());
        }
    }

    private void SearchAnother()
    {
        LVDisplay.DataSource = null;
        LVDisplay.DataBind();
        ltrtextSearch.Text = "";
        ltrProjecID.Text = "hide";
        ltrBugID.Text = "hide";
        ltrRole.Text = "hide";
    }

    private void SearchRole()
    {
        LVDisplay.DataSource = null;
        LVDisplay.DataBind();
        ltrtextSearch.Text = "hide";
        ltrProjecID.Text = "hide";
        ltrBugID.Text = "hide";
        ltrRole.Text = "";
        ddlRole.Items.Clear();
        foreach (var item in EnumUtility.EnumToList<UserManagement.Role>())
        {
            ddlRole.Items.Add(EnumUtility.GetEnumNiceName(item));
        }
    }

    protected void ddlRole_SelectedIndexChanged(object sender, EventArgs e)
    {
        int role = Convert.ToInt32(ddlRole.SelectedIndex);
        var user = from item in ApplicationGlobalVariables.KillDaBugsDatabase.Users
                   where item.Role == role
                   select new
                   {
                       item.Username,
                       item.Fullname,
                       item.IsActive,
                       //  item.User.RecentLoginDate,
                       item.AvatarImage,
                       item.Email,
                       item.Organization,
                       //  item.User.EnableEmailNotification,
                       //   item.User.BugsPerPage,
                       item.Role
                   };
        LVDisplay.DataSource = user;
        LVDisplay.DataBind();
    }
}